/*
 * ====================================================================================
 * Copyright 2012 Eric Hartwell  (http://www.ehartwell.com)
 * ====================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable
 * law or agreed to in writing, software distributed under the License is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 * ====================================================================================
 */

package com.ehartwell.samples.android.pagerview;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Wrapper for ViewPager
 */
public class PagerView extends ViewPager {

    /**
     * Creates a new PagerViewer.
     *
     * @param context The context.
     */
    public PagerView(Context context) {
        super(context);
    }

    /**
     * Creates a new PagerViewer.
     *
     * @param context context The context.
     * @param attributeSet Attributes.
     */
    public PagerView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    @Override
    /**
     * Watch events as they are dispatched to your children, and let them stop the pager from taking ownership.
     *
     * @param motionEvent The motion event being dispatched down the hierarchy.
     * @return Return true to steal motion events from the children and have them dispatched to this ViewGroup through
     * onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.
     */
    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        // Return true to steal motion events from the children only if the fragment doesn't want to handle them
        return isPagerTouchEvent(motionEvent) && super.onInterceptTouchEvent(motionEvent);
    }

    @Override
    /**
     * Implement this method to handle touch screen motion events.
     *
     * @param motionEvent The motion event.
     * @return True if the event was handled, false otherwise.
     */
    public boolean onTouchEvent(MotionEvent motionEvent) {
        // Return true to steal motion events from the children only if the fragment doesn't want to handle them
        return isPagerTouchEvent(motionEvent) && super.onTouchEvent(motionEvent);
    }

    /**
     * See if the primary (visible) fragment wants to handle this touch event.
     *
     * @param motionEvent The motion event being dispatched down the hierarchy.
     * @return <tt>True</tt> if this is a pager event, <tt>false</tt> if the primary (visible) fragment wants to handle this touch event.
     */
    private boolean isPagerTouchEvent(MotionEvent motionEvent) {
        if (getAdapter() instanceof PagerFragmentAdapter) {
            Fragment fragment = ((PagerFragmentAdapter)getAdapter()).getPrimaryItem();
            if ((fragment != null) && (fragment instanceof PagerFragment)) {
                // Give the PagerFragment a chance to override the default touch event handling (e.g. slide)
                if (((PagerFragment)fragment).isLocalTouchEvent(motionEvent)) {
                    // Pass touch events through the normal chain instead of ViewPager first.
                    return false;
                }
            }
        }
        return true;
    }

}
